package ms.jasim.model.editor.views;


import ms.jasim.framework.IMessageConsole;
import ms.jasim.model.presentation.JasimEditorPlugin;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;


/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class ConsoleView extends ViewPart implements IMessageConsole {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "ms.jasim.model.editor.views.ConsoleView";

	private Action action1;

	private ConsoleViewComposite console;

	/**
	 * The constructor.
	 */
	public ConsoleView() {
		JasimEditorPlugin.INSTANCE.getRootContext().addService(this);
	}

	@Override
	public void dispose() {
		JasimEditorPlugin.INSTANCE.getRootContext().removeService(this);
	}
	
	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	@Override
	public void createPartControl(Composite parent) {
		
		console = new ConsoleViewComposite(parent, SWT.NONE);
		
		// Create the help context id for the viewer's control
		PlatformUI.getWorkbench().getHelpSystem().setHelp(console, "ms.jasim.model.editor.viewer");
		makeActions();
		hookContextMenu();
		contributeToActionBars();
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				ConsoleView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(console);
		console.setMenu(menu);
		//getSite().registerContextMenu(menuMgr, console);
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(action1);
	}

	private void fillContextMenu(IMenuManager manager) {
		manager.add(action1);
		// Other plug-ins can contribute there actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}
	
	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(action1);
	}

	private void makeActions() {
		action1 = new Action() {
			@Override
			public void run() {
				clearAll();
			}
		};
		action1.setText("Clear");
		action1.setToolTipText("Clear console text");
		action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_ETOOL_CLEAR));
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	@Override
	public void setFocus() {
		console.setFocus();
	}

	@Override
	public void write(String message) {
		write("(default)", message);
	}

	public void write(String session, String message) {
		try {
			IDocument doc = console.getDocument(session);
			doc.replace(doc.getLength(), 0, message);
			console.activateSession(session, false);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}	
	}
	
	public void clearAll() {
		console.clearAll();
	}
	
}